const { ref } = Vue
import { useFetch } from '../../../Scripts/hooks/useFetch.js'

export function useEmployeeWorkdate() {
  let employeeWorkDate = ref({ today: '', tomorrow: '' });// 排班信息

  const getEmployeeWorkdate = async configObj => {
    const { data } = await useFetch({
      url: `${configObj.url}?actions=${configObj.method}`,
      data: {
        type: `${configObj.method}`,
        employeeid: configObj.employeeid
      }
    });
    if (data.value.length === 2) {
      for (const i of data.value) {
        const startdate = dayjs(i.startdate);
        let enddate = dayjs(i.enddate);
        // 结束时间 < 开始时间时，表示跨天
        if (enddate.diff(startdate) < 0) {
          enddate = dayjs(enddate).add(1, 'day');// 结束时间 +1 天
        }

        const diffInHours = (enddate.diff(startdate)) / (1000 * 60 * 60);

        if (i.datatype === 'today') {
          employeeWorkDate.value.today = `${i.workshiftname} ${diffInHours.toFixed(2)}小时`;
        } else {
          employeeWorkDate.value.tomorrow = `${i.workshiftname} ${diffInHours.toFixed(2)}小时`;
        }
      }
    } else if (data.value.length === 1) {
      const startdate = dayjs(data.value[0].startdate);
      let enddate = dayjs(data.value[0].enddate);
      // 结束时间 < 开始时间时，表示跨天
      if (enddate.diff(startdate) < 0) {
        enddate = dayjs(enddate).add(1, 'day');// 结束时间 +1 天
      }
      const diffInHours = (enddate.diff(startdate)) / (1000 * 60 * 60);
      if (data.value[0].datatype === 'today') {
        employeeWorkDate.value.today = `${data.value[0].workshiftname} ${diffInHours.toFixed(2)}小时`;
        employeeWorkDate.value.tomorrow = '暂无排班';
      } else {
        employeeWorkDate.value.today = '暂无排班';
        employeeWorkDate.value.tomorrow = `${data.value[0].workshiftname} ${diffInHours.toFixed(2)}小时`;
      }
    } else {
      employeeWorkDate.value.today = '暂无排班';
      employeeWorkDate.value.tomorrow = '暂无排班';
    }
  };

  return { employeeWorkDate, getEmployeeWorkdate };
}